<?php

defined('SYSPATH') or die('Access error!');

class Controller_Shop_Main extends Controller_Main
{

    public function before()
    {
        parent::before();
        $this->template->assign('media_path', URL::site($this->template->media_path) . '/');
        Assets::css('message', $this->template->media_path . 'css/message.css');
        Assets::css('shop_gx_style', $this->template->media_path . 'js/bx_styles/bx_styles.css');
        Assets::css('shop_global', $this->template->media_path . 'shop/css/global.css');
        Assets::css('shop_home', $this->template->media_path . 'shop/css/home.css');
        Assets::js('focus', $this->template->media_path . 'shop/js/jquery.focus.js');
        Assets::js('bxslider', $this->template->media_path . 'shop/js/jquery.bxSlider.min.js');
        Assets::js('idtabs', $this->template->media_path . 'shop/js/jquery.idTabs.js');
        Assets::css('jnotify', $this->template->media_path . 'js/fancybox/fancybox.css');
        Assets::js('jnotify', $this->template->media_path . 'js/fancybox/jquery.fancybox-1.3.1.pack.js');
        Assets::js('message', $this->template->media_path . 'js/message.js');
    }

    public function after()
    {
        Assets::remove_css('form');
        Assets::remove_js('validate');
        parent::after();
    }

    //获取产品分类的子分类(一级)
    public function get_sub_category($category_id = SHOP_CATEGORY_ID)
    {
        $category_list = Model_Category::factory('category')->get_category_list('parent_id=' . $category_id);

        return Arr::validate_arr($category_list) ? $category_list : array();
    }

    //根据分类的id获取对应的广告
    public function get_ads_by_category_id($ads_category_id)
    {
        Model_Ads::factory('ads')->get_ads_by_category_id($ads_category_id);
    }

    public function create_time_path()
    {
        return date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d') . DIRECTORY_SEPARATOR;
    }



    //获取某个分类下面的子分类（二级）
    public function get_double_category($id)
    {
        $id = intval($id);
        $category_list_temp = Model_Category::factory('category')->get_category_list('parent_id=' . $id);
        $category_list = array();
        if (Arr::validate_arr($category_list_temp))
        {
            foreach ($category_list_temp as $key => $value)
            {
                if (isset($value['id']))
                {
                    $value['children'] = Model_Category::factory('category')->get_category_list('parent_id=' . $value['id']);
                }
                $category_list[] = $value;
            }
        }
        unset($category_list_temp);
        return $category_list;
    }

    /**
     *  获取当前位置(分类的)
     * @param type $id
     */
    public function set_category_position($id)
    {
        $category_position = Model_Category::factory('category')->get_category_position($id, 134);

        if (Arr::validate_arr($category_position))
        {
            foreach ($category_position as $value)
            {
                if ($value['id'] == 52)
                {
                    Breadcrumbs::add('商城', URL::site('shop/index'));
                }
                else
                {
                    Breadcrumbs::add($value['name'], URL::site('shopnews/list/' . $value['id']));
                }
            }
        }
    }

    public function set_goods_category_position($id)
    {
             $category_position = Model_Category::factory('category')->get_category_position($id, SHOP_CATEGORY_ID);

        if (Arr::validate_arr($category_position))
        {
            foreach ($category_position as $value)
            {
                if ($value['id'] == SHOP_CATEGORY_ID)
                {
                    Breadcrumbs::add('商城', URL::site('shop/index'));
                }
                else
                {
                    Breadcrumbs::add($value['name'], URL::site('goods/list/' . $value['id']));
                }
            }
        }
    }
    
      /**
     *  获取分类的SEO数据
     * @param type $id
     * @return type
     */
    public function category_seo($id)
    {
        $category_seo = Model_Category::factory('category')->get_category_seo($id, SHOP_CATEGORY_ID);
        return $category_seo;
    }

    /**
     *  获取详细内容的seo数据
     * @param type $id
     * @param type $keyword
     * @param type $description
     */
    public function detail_seo($id, $keyword, $description)
    {
        //获取问答的分类id
        $goods= Model::factory('shop_goods')->get_row(array('id' => $id), 'category_id,name');
        $detail_seo_temp = $this->category_seo($goods['category_id']);
        $detail_seo_title = array();
        if (Arr::validate_arr($detail_seo_temp['title']))
        {
            $detail_seo_title = $detail_seo_temp['title'];
            $detail_seo_title = array_reverse($detail_seo_title);
            array_push($detail_seo_title, $goods['name']);
        }
        $detail_seo_title = array_reverse($detail_seo_title);

        $seo_arr = array(
            'title' => $detail_seo_title,
            'keywords' => $keyword,
            'description' => $description,
        );

        $this->set_seo($seo_arr);
    }

}